import Vue from 'vue'
import VueRouter from 'vue-router'
import store from "@/store";

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/system/login/Login.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

// 注意：刷新页面会导致页面路由重置
export const setRoutes = () => {
  const storeMenus = localStorage.getItem("menus")
  
  if (storeMenus) {
    // 拼装动态路由
    const manageRoute = { path:'/', name:'Manage', component: () => import('../views/Manage.vue'), redirect: '/home', children: [] }
    const menus = JSON.parse(storeMenus)
    menus.forEach(item =>{
      if (item.path) { // 当且仅当path不为空的时候才去设置路由
        let itemMenu = { path: item.path.replace("/",""), name: item.mname, component: () => import('../views/system' + item.path + '/' + item.pagePath + '.vue') }
        manageRoute.children.push(itemMenu)
        manageRoute.children.push({
          path: '/courseInfo:id',
          name: 'courseInfo',
          component: () => import('../views/system/course/CourseInfo.vue')
        }),
        manageRoute.children.push({
          path: '/details',
          name: 'details',
          component: () => import('../views/system/frontdeskuserdetails/Details.vue')
        }),
        manageRoute.children.push({
          path: '/memberManagerInfo',
          name: 'memberManagerInfo',
          component: () => import('../views/system/memberManager/MemberManagerInfo.vue')
        }),
        manageRoute.children.push({
          path: '/person',
          name: '个人中心',
          component: () => import('../views/system/user/Person.vue')
        })
      } else if (item.children.length) {
        item.children.forEach(item => {
          if (item.path) {
            let itemMenu = { path:item.path.replace("/",""), name: item.mname, component: () => import('../views/system' + item.path + '/' + item.pagePath + '.vue') }
            manageRoute.children.push(itemMenu)
          }
        })
      }
    })

    // 获取当前路由对象名称数组
    const currentRouteNames = router.getRoutes().map(v => v.name)
    if (!currentRouteNames.includes('Manage')) {
      // 动态添加到现在的路由对象中去
    router.addRoute(manageRoute)
    }
  }
}

// 重置就在set一下
setRoutes()

router.beforeEach((to, from, next) => {
  localStorage.setItem("currentPathName", to.name)  // 设置当前的路由名称
  store.commit("setPath")

  // 未找到路由的情况
  if (!to.matched.length) {
    const storeMenus = localStorage.getItem("menus")
    if (storeMenus) {
      next("/home")
    } else {
      // 跳回登录页面
      next("/login")
    }
  }
  // 其他的情况都放行
  next()

})

// 提供一个重置路由的方法
export const resetRouter = () => {
  router.matcher = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
  })
}


// 全局后置钩子函数 //王勃
router.afterEach((to,from) => {
  // 在路由跳转完成后执行的方法
  if (from.path !== '/' && to.path === '/map' ) {
    methodToExecuteAfterNavigation();
  }
});
// 全局方法
function methodToExecuteAfterNavigation() {
  // 路由跳转完成后执行的方法
  window.location.reload(); //监测到路由发生跳转时刷新一次页面
  console.log("After navigation");
}

export default router